--
-- Author: shunguo.chen
-- Date: 2019-07-02 11:42:02
--
-- AvatarShopInfo 理发店商品信息
-- 

local AvatarShopItem = import(".AvatarShopItem")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
    if data then
        self:init(data)
    end
end

-- data = conf barber_shop表conf
function ClassRef:init(conf)
    self._hairColorItemDict = {}
    
    local faceIDs = table.tonumber(string.split(conf.faceIDs, ";"))
    local faceItems = {}
    for i,faceID in ipairs(faceIDs) do
        faceItems[i] = AvatarShopItem.new(faceID, Enums.AvatarShopType.face)
    end

    local hairIDs = table.tonumber(string.split(conf.hairIDs, ";"))
    local hairItems = {}
    local item, hairColorIds, hairColorItems, hairColorItem
    for i,hairID in ipairs(hairIDs) do
        item = AvatarShopItem.new(hairID, Enums.AvatarShopType.hair)
        hairItems[i] = item

        -- init hairColorItems
        hairColorIds = item:getHairColorIds()
        hairColorItems = {}
        for j,hairColorId in ipairs(hairColorIds) do
            hairColorItem = self:_getHairColorItem(hairColorId)
            hairColorItems[j] = hairColorItem
            self._hairColorItemDict[hairColorId] = hairColorItem
        end
        item:setHairColorItems(hairColorItems)
    end

    local genders = {1,2}
    local genderItems = {}
    for i,gender in ipairs(genders) do
        genderItems[i] = AvatarShopItem.new(gender, Enums.AvatarShopType.gender)
    end

    self._faceItems = faceItems
    self._hairItems = hairItems
    self._genderItems = genderItems

    -- dump(faceItems, "faceItems")
    -- dump(hairItems, "hairItems")
    -- dump(genderItems, "genderItems")
    -- dump(self._hairColorItemDict, "self._hairColorItemDict")
end

-- message S2C_BarberShopUnlock{
--     repeated int32 hairList = 1;
--     repeated int32 hairColorList = 2;
--     repeated int32 faceList = 3;
-- }
function ClassRef:initMsg(msg)
    local activeFaceIdDict = {}
    local activeHairIdDict = {}
    local activeHairColorIdDict = {}

    for i,id in ipairs(msg.faceList) do
        activeFaceIdDict[id] = true
    end
    for i,id in ipairs(msg.hairList) do
        activeHairIdDict[id] = true
    end
    for i,id in ipairs(msg.hairColorList) do
        activeHairColorIdDict[id] = true
    end

    self._activeFaceIdDict = activeFaceIdDict
    self._activeHairIdDict = activeHairIdDict
    self._activeHairColorIdDict = activeHairColorIdDict

    for i,item in ipairs(self._faceItems) do
        if activeFaceIdDict[item:getId()] then
            item:setActive(true)
        end
    end

    local hairColorItems
    for i,item in ipairs(self._hairItems) do
        if activeHairIdDict[item:getId()] then
            item:setActive(true)
        end

        hairColorItems = item:getHairColorItems()
        for i,hairColorItem in ipairs(hairColorItems) do
            if activeHairColorIdDict[hairColorItem:getId()] then
                hairColorItem:setActive(true)
            end
        end
    end
end

function ClassRef:_getHairColorItem(id)
    local item = self._hairColorItemDict[id]
    if not item then
        item = AvatarShopItem.new(id, Enums.AvatarShopType.hairColor)
    end
    return item
end

-- //理发店 会被解锁的物品类型
-- enum BarberUnlockType{
--     face = 0;
--     hair = 1;
--     hairColor = 2;
-- }
-- //新解锁红点实时更新
-- message S2C_BarberUnSeeRedPointUpdate{
--     BarberUnlockType typ = 1;//新解锁的理发店 类型
--     int32    typId = 2;      //对应的ID
-- }
function ClassRef:unlockItem(msg)
    local id = msg.typId
    if msg.typ == "face" then
        self._activeFaceIdDict[id] = true
        for i,item in ipairs(self._faceItems) do
            if item:getId() == id then
                item:setActive(true)
                item:setIsNew(true)
                break
            end
        end
    elseif msg.typ == "hair" then
        self._activeHairIdDict[id] = true
        for i,item in ipairs(self._hairItems) do
            if item:getId() == id then
                item:setActive(true)
                item:setIsNew(true)
                break
            end
        end
    elseif msg.typ == "hairColor" then
        self._activeHairColorIdDict[id] = true
        for k,item in pairs(self._hairColorItemDict) do
            if item:getId() == id then
                item:setActive(true)
                item:setIsNew(true)
                break
            end
        end
    end
end

function ClassRef:getFaceItems()
    return self._faceItems
end

function ClassRef:getHairItems()
    return self._hairItems
end

function ClassRef:getHairColorItems(hairID)
    local hairItem = self:getHairItemById(hairID)
    return hairItem:getHairColorItems()
end

function ClassRef:getGenderItems()
    return self._genderItems
end

function ClassRef:getFaceItemByID(faceID)
    if not faceID then
        return self._faceItems[1]
    end
    for i,item in ipairs(self._faceItems) do
        if item:getId() == faceID then
            return item
        end
    end
    return self._faceItems[1]
end

function ClassRef:getHairItemByID(hairID)
    if not hairID then
        return self._hairItems[1]
    end
    for i,item in ipairs(self._hairItems) do
        if item:getId() == hairID then
            return item
        end
    end
    return self._hairItems[1]
end

function ClassRef:getHairColorItemByID(hairID, hairColorID)
    local hairColorItems = self:getHairColorItems(hairID)
    if not hairColorID then
        return hairColorItems[1]
    end
    for i,item in ipairs(hairColorItems) do
        if item:getId() == hairColorID then
            return item
        end
    end
    return hairColorItems[1]
end

function ClassRef:clearNew()
    for i,item in ipairs(self._faceItems) do
        if item:isNew() then
            item:setIsNew(false)
        end
    end
    for i,item in ipairs(self._hairItems) do
        if item:isNew() then
            item:setIsNew(false)
        end
    end
    for k,item in pairs(self._hairColorItemDict) do
        if item:isNew() then
            item:setIsNew(false)
        end
    end
end

function ClassRef:isNew()
    local isFaceNew = self:isFaceNew()
    local isHairNew = self:isHairNew()
    if isFaceNew or isHairNew then
        return true
    else
        local player = UD:getPlayer()
        local hairID = player.hairID
        return self:isHairColorNew(hairID)
    end
end

function ClassRef:isFaceNew()
    for i,item in ipairs(self._faceItems) do
        if item:isNew() then
            return true
        end
    end
    return false
end

function ClassRef:isHairNew()
    for i,item in ipairs(self._hairItems) do
        if item:isNew() then
            return true
        end
    end
    return false
end

function ClassRef:isHairColorNew(hairID)
    local hairItem = self:getHairItemById(hairID)
    if hairItem then
        return hairItem:isHairColorNew()
    end
    return false
end

function ClassRef:getHairItemById(id)
    for i,item in ipairs(self._hairItems) do
        if item:getId() == id then
            return item
        end
    end
end

-- UserBaseInfo里面hairColorID 取值1-5 对应这里的hairColorIndex 
-- 配置和pb里面的hairColorID haircolor_res表的id
function ClassRef:getHairColorIndex(hairID, hairColorID)
    local hairItem = self:getHairItemById(hairID)
    return hairItem:getHairColorIndex(hairColorID)
end
function ClassRef:getHairColorID(hairID, hairColorIndex)
    local hairItem = self:getHairItemById(hairID)
    return hairItem:getHairColorID(hairColorIndex)
end

return ClassRef